package com.sanshi.project.procure.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sanshi.project.admin.api.dto.ProcureGoodsVendorDTO;
import com.sanshi.project.admin.api.entity.ProcureGoodsVendorEntity;
import com.sanshi.project.common.core.util.R;
import com.sanshi.project.common.excel.annotation.ResponseExcel;
import com.sanshi.project.common.log.annotation.SysLog;
import com.sanshi.project.procure.service.ProcureGoodsVendorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物品供应商
 *
 * @author nikon
 * @date 2023-12-08 17:06:22
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/procureGoodsVendor")
@Tag(description = "procureGoodsVendor", name = "物品供应商管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ProcureGoodsVendorController {

    private final ProcureGoodsVendorService procureGoodsVendorService;

    /**
     * 分页查询
     *
     * @param page               分页对象
     * @param procureGoodsVendor 物品供应商
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('procure_procureGoodsVendor_view')")
    public R getProcureGoodsVendorPage(@ParameterObject Page page, @ParameterObject ProcureGoodsVendorDTO procureGoodsVendor) {
        LambdaQueryWrapper<ProcureGoodsVendorEntity> wrapper = Wrappers.lambdaQuery();
        // 供应商名称
        wrapper.like(StrUtil.isNotBlank(procureGoodsVendor.getName()), ProcureGoodsVendorEntity::getName, procureGoodsVendor.getName());
        // 联系人姓名
        wrapper.like(StrUtil.isNotBlank(procureGoodsVendor.getContact()), ProcureGoodsVendorEntity::getContact, procureGoodsVendor.getContact());
        // 供应商联系方式
        wrapper.like(StrUtil.isNotBlank(procureGoodsVendor.getTelephone()), ProcureGoodsVendorEntity::getTelephone, procureGoodsVendor.getTelephone());
        // 供应商地址
        wrapper.like(StrUtil.isNotBlank(procureGoodsVendor.getAddress()), ProcureGoodsVendorEntity::getAddress, procureGoodsVendor.getAddress());
        // 开户行
        wrapper.like(StrUtil.isNotBlank(procureGoodsVendor.getBankDeposit()), ProcureGoodsVendorEntity::getBankDeposit, procureGoodsVendor.getBankDeposit());
        // 银行账号
        wrapper.like(StrUtil.isNotBlank(procureGoodsVendor.getBankAccount()), ProcureGoodsVendorEntity::getBankAccount, procureGoodsVendor.getBankAccount());
        wrapper.orderByDesc(ProcureGoodsVendorEntity::getUpdateTime);
        return R.ok(procureGoodsVendorService.page(page, wrapper));
    }


    /**
     * 通过id查询物品供应商
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('procure_procureGoodsVendor_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(procureGoodsVendorService.getById(id));
    }

    /**
     * 新增物品供应商
     *
     * @param procureGoodsVendor 物品供应商
     * @return R
     */
    @Operation(summary = "新增物品供应商", description = "新增物品供应商")
    @SysLog("新增物品供应商")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('procure_procureGoodsVendor_add')")
    public R save(@RequestBody ProcureGoodsVendorEntity procureGoodsVendor) {
        return R.ok(procureGoodsVendorService.save(procureGoodsVendor));
    }

    /**
     * 修改物品供应商
     *
     * @param procureGoodsVendor 物品供应商
     * @return R
     */
    @Operation(summary = "修改物品供应商", description = "修改物品供应商")
    @SysLog("修改物品供应商")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('procure_procureGoodsVendor_edit')")
    public R updateById(@RequestBody ProcureGoodsVendorEntity procureGoodsVendor) {
        return R.ok(procureGoodsVendorService.updateById(procureGoodsVendor));
    }

    /**
     * 通过id删除物品供应商
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除物品供应商", description = "通过id删除物品供应商")
    @SysLog("通过id删除物品供应商")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('procure_procureGoodsVendor_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(procureGoodsVendorService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 导出excel 表格
     *
     * @param procureGoodsVendor 查询条件
     * @param ids                导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('procure_procureGoodsVendor_export')")
    public List<ProcureGoodsVendorEntity> export(ProcureGoodsVendorEntity procureGoodsVendor, Long[] ids) {
        return procureGoodsVendorService.list(Wrappers.lambdaQuery(procureGoodsVendor).in(ArrayUtil.isNotEmpty(ids), ProcureGoodsVendorEntity::getId, ids));
    }

    /**
     * 供应商管理全量查询
     *
     * @param procureGoodsVendor 供应商管理
     * @return
     */
    @Operation(summary = "供应商管理全量查询", description = "供应商管理全量查询")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('procure_procureGoodsVendor_view')")
    public R getProcureGoodsVendorList(@ParameterObject ProcureGoodsVendorDTO procureGoodsVendor) {
        LambdaQueryWrapper<ProcureGoodsVendorEntity> wrapper = Wrappers.lambdaQuery();
        // 供应商名称
        wrapper.like(StrUtil.isNotBlank(procureGoodsVendor.getName()), ProcureGoodsVendorEntity::getName, procureGoodsVendor.getName());
        // 联系人姓名
        wrapper.like(StrUtil.isNotBlank(procureGoodsVendor.getContact()), ProcureGoodsVendorEntity::getContact, procureGoodsVendor.getContact());
        // 供应商联系方式
        wrapper.like(StrUtil.isNotBlank(procureGoodsVendor.getTelephone()), ProcureGoodsVendorEntity::getTelephone, procureGoodsVendor.getTelephone());
        // 供应商地址
        wrapper.like(StrUtil.isNotBlank(procureGoodsVendor.getAddress()), ProcureGoodsVendorEntity::getAddress, procureGoodsVendor.getAddress());
        // 开户行
        wrapper.like(StrUtil.isNotBlank(procureGoodsVendor.getBankDeposit()), ProcureGoodsVendorEntity::getBankDeposit, procureGoodsVendor.getBankDeposit());
        // 银行账号
        wrapper.like(StrUtil.isNotBlank(procureGoodsVendor.getBankAccount()), ProcureGoodsVendorEntity::getBankAccount, procureGoodsVendor.getBankAccount());
        return R.ok(procureGoodsVendorService.list(wrapper));
    }
}